/**
 * Copyright © 2020 易久批电子商务有限公司. All rights reserved.
 */
package com.dysm.imageprint;

import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.gif.GIFImageReaderSpi;

import javax.imageio.ImageIO;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.FileImageInputStream;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * 此类可以读取一张GIF图片，动态输出到控制台
 * @author xiay
 * @since 2020/9/18 10:00 上午
 */
public class GifImagePrintTest {


    public static void main(String[] args) throws Exception {
        String filePath = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("loading.gif")).getFile();
        GifImagePrintTest test = new GifImagePrintTest();
        GIFImageReader reader = test.readGif(filePath);
        int frame = reader.getNumImages(true);

        test.printGif(reader, frame, 0.4);
//        test.convertStaticImages(reader, frame, srcPath);
    }

    /**
     * 根据文件路径读取gif图片
     * @param filePath 文件路径
     * @return
     * @throws IOException
     */
    private GIFImageReader readGif(String filePath) throws IOException {
        File f2 = new File(filePath);
        FileImageInputStream in = new FileImageInputStream(f2);
        ImageReaderSpi readerSpi = new GIFImageReaderSpi();
        GIFImageReader gifReader = (GIFImageReader) readerSpi.createReaderInstance();

        gifReader.setInput(in);
        return gifReader;
    }

    /**
     * 将GIF图片转换成一帧一帧的静态图片
     * @param reader
     * @param frame
     * @throws IOException
     */
    private void convertStaticImages(GIFImageReader reader, int frame, String srcPath) throws IOException {
        for (int i = 0; i < frame; i++) {
            BufferedImage image = reader.read(i);
            File out = new File(srcPath + (i + 1) +".gif");
            ImageIO.write(image, "GIF", out);
        }
    }

    /**
     * 控制台打印图片
     * @param reader
     * @param frame
     * @param resize
     * @throws IOException
     */
    private void printGif(GIFImageReader reader, int frame, double resize) throws Exception {
        if (resize == 0) {
            throw new RuntimeException("resize must not be zero !");
        }
        for (int i = 0; i < frame; i++) {
            BufferedImage image = reader.read(i);
            BufferedImage resizeImage = HandleImageSize.resizeImage(image, (int)(image.getWidth() * resize), 0);
            Print.printImage(resizeImage, 0, true, System.out);
            Thread.sleep(100L);
//            clearConsole();
        }
    }

    /**
     * 可以通过调用此方法，清除控制台内容
     * 清除方式不是很优雅，先通过设置鼠标左键点击将焦点定位到控制台，再通过快捷键  Ctrl + Alt + R 点击 clear all按钮清除控制台
     * 此方式存在一些问题：1、sleep时间过短，控制台还未完成输出当前图片就已被清除，sleep时间过长GIF动画不够连续
     *                   2、由于使用了鼠标点击事件进行焦点定位，故运行时鼠标必须放置到控制台区域，否则无效
     * @throws Exception
     */
    private static void clearConsole() throws Exception {
        Robot bot = new Robot();
        bot.mousePress(InputEvent.BUTTON1_MASK);
        bot.mouseRelease(InputEvent.BUTTON1_MASK);

        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_R);

        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.keyRelease(KeyEvent.VK_ALT);
        bot.keyRelease(KeyEvent.VK_R);
    }


}
